package com.mx.kn.cstf2.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import kn.cstf2.transition.Bean.TransitionBean;
import kn.mx.cstf2.sql.TransitionSql;

import org.eclipse.jdt.core.dom.SwitchCase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.mx.kn.cstf2.dao.ForoDao;
import com.mx.kn.cstf2.dto.ComentarioDto;
import com.mx.kn.cstf2.dto.ForoDto;



@Repository
@Component("foroDao")
public class ForoDaoImpl implements ForoDao{

	private JdbcTemplate jdbcStksf2;
	private JdbcTemplate jdbcCustomerSalesToolKit;

	@Autowired
	public void setDataSourceStkfsf2(@Qualifier("dataSourceStkfsf2") DataSource DataSourceStkfsf2) {
		this.jdbcStksf2 = new JdbcTemplate(DataSourceStkfsf2);
	}
	
	@Autowired
	public void setDataSourceCustomerSalesToolKit(@Qualifier("dataSourceCustomerSalesToolKit")
			DataSource dataSourceCustomerSalesToolKit) {
		this.jdbcCustomerSalesToolKit = new JdbcTemplate(dataSourceCustomerSalesToolKit);
		
	}
	
	@Override
	public List<ForoDto> getTopics(TransitionBean transition) throws SQLException {
		String sqlFs = "select topicos.*,foro_topico.\"idForo\", foro_topico.last_date , foro_topico.\"lastUser\" from foro_topico "+
						" INNER JOIN topicos ON(foro_topico.\"idTopico\" = topicos.id_topico)"+
					    " WHERE foro_topico.\"idForo\" = ? AND fs='t'"+
					    " ORDER BY id_topico";
		 
		
		String sqlFo = "select topicos.*,foro_topico.\"idForo\", foro_topico.last_date from foro_topico "+
				" INNER JOIN topicos ON(foro_topico.\"idTopico\" = topicos.id_topico)"+
			    " WHERE foro_topico.\"idForo\" = ? AND fo='t'"+
			    " ORDER BY id_topico";
		
		
		String sqlFa = "select topicos.* ,foro_topico.\"idForo\", foro_topico.last_date from foro_topico "+
				" INNER JOIN topicos ON(foro_topico.\"idTopico\" = topicos.id_topico)"+
			    " WHERE foro_topico.\"idForo\" = ? AND fa='t'"+
			    " ORDER BY id_topico";
		List<ForoDto> foros = null;
		switch (transition.getTipoTransition()) {
		case 1:
			foros = jdbcStksf2.query(
					sqlFs,new Object[] { transition.getIdForo()} ,new BeanPropertyRowMapper(ForoDto.class));
			break;

		case 2:
			foros = jdbcStksf2.query(
					sqlFo, new Object[] { transition.getIdForo()}  ,new BeanPropertyRowMapper(ForoDto.class));
			break;
			
		case 3:
			foros = jdbcStksf2.query(
					sqlFa, new Object[] { transition.getIdForo()} ,new BeanPropertyRowMapper(ForoDto.class));
			break;
		default:
			break;
		}
		
		
	 
		return foros;
	}

	@Override
	public List<ComentarioDto> getListComentarios(int idTopico, int idForo)throws SQLException{
	
		List<ComentarioDto> comentarios = getChildrenComentario(idTopico,0, idForo);
		return comentarios;
	}
	
	private List<ComentarioDto> getChildrenComentario(int idTopico, int idComentarioPadre, int idForo){
		String sql="SELECT * FROM comentarios WHERE id_topico= ? AND id_comentario_padre=? AND \"idForo\" = ?";
		List<ComentarioDto> enviar = new ArrayList<ComentarioDto>(); 
		List<ComentarioDto> comentarios = jdbcStksf2.query(sql, new Object[] { idTopico, idComentarioPadre, idForo }, new BeanPropertyRowMapper(ComentarioDto.class));
		for(ComentarioDto comentario : comentarios){
			comentario.setSubComentarios(getChildrenComentario(idTopico, comentario.getId(),idForo));
			enviar.add(comentario);
		}
		return enviar;
	}
	
	@Override
	public void insertComment(int idTopico, int idComentarioPadre,int idForo, String comentario, String autor)throws SQLException{
		String sql="INSERT INTO comentarios (id_topico,id_comentario_padre,comentario, fecha_comentario,autor, \"idForo\") VALUES(?,?,?,now(),?,?);";
		int rows = jdbcStksf2
				.update(sql, new Object[] {
						idTopico, idComentarioPadre, comentario, autor, idForo });
		
	}
	
	@Override
	public void insertLastUpdateForo(Date date, String user, ForoDto foro){
		String sql = "UPDATE foro_topico set last_date = ?, \"lastUser\" = ? where \"idForo\" = ? and \"idTopico\" = ?";
		int rows = jdbcStksf2.update(sql, new Object[] {date, user, foro.getIdForo(), foro.getId_topico()});
	}
	
	@Override
	public void updateUrlImgComment(int idTopico, int idComentario,int idForo, String urlImg)throws SQLException{
		String sql="update comentarios set url_img = ?, img='T', \"idForo\" = ? where id_topico = ? and id = ?";
		int rows = jdbcStksf2
				.update(sql, new Object[] {
						urlImg, idForo,idTopico, idComentario,  });
		
	}
	
	@Override
	public void updateUrlFileComment(int idTopico, int idComentario,int idForo, String urlFile)throws SQLException{
		String sql="update comentarios set url_file = ?,file='T', \"idForo\" = ? where id_topico = ? and id = ?";
		int rows = jdbcStksf2
				.update(sql, new Object[] {
						urlFile, idForo,idTopico, idComentario});
		
	}
	
	@Override
	public ComentarioDto getComentartio(int idTopico, int idComentario, TransitionBean transition) throws SQLException{
		String sql = "SELECT * FROM comentarios where id_topico = ?  AND id = ?";
		
		return (ComentarioDto)jdbcStksf2.queryForObject(
				sql, new Object[] { idTopico,idComentario}, new BeanPropertyRowMapper(ComentarioDto.class));
		

	}
	
	
	
	
}
